<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017\10\30 0030
 * Time: 17:33
 */

class Base extends CI_Controller
{
    protected $redis = [];//redis数据
    protected $root = '';//root账号
    protected $account = '';//当前账号

    public function __construct()
    {
        parent::__construct();

        //判断登录情况
        if(!isset($_SESSION['user_name']) || empty($_SESSION['user_name'])){
            $this->load->helper('url');
            redirect('Entrance/login');
        }

        $this->account = $_SESSION['user_name'];

        $redisData = $this->cache->redis->gets('authority','root',$this->account);

        $this->redis['authority'] = $redisData['authority'];
        $this->redis['root'] = $redisData['root'];
        $this->redis[$this->account] = $redisData[$this->account];


        $this->refresh_redis('all');


        if(method_exists($this,'_initialize')){
            $this->_initialize();
        }
        //@view()->share();

    }



    //刷新Redis数据
    protected function refresh_redis($key = '')
    {
        $is_write  = 0;

        //清空指定数据
        if($key){
            //Log::log('debug',$this->redis[$key]);
            $is_write = 1;
            if( 'all' == $key){
                $this->redis = null;
            }else{
                $this->redis[$key] = null;
            }
        }

        //所有菜单
        if(!isset($this->redis['authority']) || !$this->redis['authority']){
            $is_write = 1;
            $this->redis['authority'] = $this->get_menu_tree($this->get_authority());
        }

        //root账号
        if(!isset($this->redis['root']) || !$this->redis['root'] || ($this->redis['root'] != $this->config->item('root'))){
            $is_write = 1;
            $this->root = $this->redis['root'] = $this->config->item('root');
        }else{
            $this->root = $this->redis['root'];
        }

        //当前用户信息
        if(!isset($this->redis[$this->account]) || !$this->redis[$this->account]){
            $is_write = 1;
            $this->redis[$this->account] = $this->get_userInfo();
        }

        if($is_write){
            $this->cache->redis->saves([
                'authority'=>$this->redis['authority'],
                'root'=>$this->redis['root'],
                $this->account => $this->redis[$this->account]
                ]);
        }
    }

    //获取用户信息
    protected function get_userInfo()
    {
        $this->load->database();

        $user = $this->db->query('SELECT * FROM admin_user AS u
INNER JOIN admin_user_role r ON r.user_id = u.id
WHERE u.account = "'.$this->account.'" LIMIT 1');
        $user = $user->result();

        if(!$user->role_id){
            return [];
        }

        $user_info = [];
        $user_info['id'] = $user->id;
        $user_info['account'] = $user->account;
        $user_info['nickname'] = $user->nickname;
        $user_info['role_id'] = $user->role_id;

        if($this->account == $this->root){
            //最高权限
            $authority = $this->get_authority();
        }else{
            //个人权限
            $authority = $this->get_authority($user->role_id);
        }

        $list = $this->get_menu_tree($authority);
        $rule = $this->get_rule($authority);

        $user_info['authority'] = $list;
        $user_info['rule'] = $rule;

        return $user_info;
    }



    //获取权限
    protected function get_authority($role_id = '')
    {
        $this->load->database();
        if($role_id){
            $arr = $this->db->query('SELECT a.id AS a_id,a.name AS a_name,a.code AS a_code,a.module_id AS a_module_id,a.classify_id AS a_classify_id,m.name AS m_name,m.icon AS m_icon,c.name AS c_name,
c.route AS c_route,c.func AS c_func,c.sort AS c_sort,m.sort AS m_sort
FROM admin_role_authority AS ra 
LEFT JOIN admin_module AS m ON m.id = ra.module_id
LEFT JOIN admin_classify AS c ON c.id=ra.classify_id
LEFT JOIN admin_authority AS a ON a.id=ra.authority_id
WHERE ra.role_id = "'.$role_id.'" 
ORDER BY m.sort DESC,c.sort DESC,c.id ASC');
        }else{
            $arr = $this->db->query('SELECT a.id AS a_id,a.name AS a_name,a.code AS a_code,a.module_id AS a_module_id,a.classify_id AS a_classify_id,m.name AS m_name,m.icon AS m_icon,c.name AS c_name,
c.route AS c_route,c.func AS c_func,c.sort AS c_sort,m.sort AS m_sort
FROM admin_authority AS a
LEFT JOIN admin_module AS m ON a.module_id=m.id
LEFT JOIN admin_classify AS c ON c.id=a.classify_id
ORDER BY m.sort DESC,c.sort DESC,c.id ASC');
        }

        return $arr->result_array();
    }


    //获取用户规则
    private function get_rule($arr)
    {
        $rule = [];
        foreach($arr as $k => $v){
            $rule[$k]['route'] = $v['c_route'];
            $rule[$k]['code'] = $v['a_code'];
        }
        return $rule;
    }

    //获取菜单树状图
    private function get_menu_tree($arr)
    {
        $list = [];

        //构建菜单树状图
        foreach($arr as $k => $v){
            $list[$v['a_module_id']]['m_id'] = $v['a_module_id'];
            $list[$v['a_module_id']]['m_name'] = $v['m_name'];
            $list[$v['a_module_id']]['m_icon'] = $v['m_icon'];
            $list[$v['a_module_id']]['arr'] = [];
        }
        foreach($list as $kk => $vv){
            $s = 0;
            foreach($arr as $kkk => $vvv){
                if($vvv['a_module_id'] == $vv['m_id']){
                    $list[$kk]['arr'][$vvv['a_classify_id']]['c_id'] = $vvv['a_classify_id'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['c_name'] = $vvv['c_name'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['c_route'] = $vvv['c_route'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['c_func'] = $vvv['c_func'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['c_sort'] = $vvv['c_sort'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['is_check'] = 0;
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['name'] = $vvv['a_name'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['module_id'] = $vvv['a_module_id'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['classify_id'] = $vvv['a_classify_id'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['code'] = $vvv['a_code'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['id'] = $vvv['a_id'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['is_check'] = 0;
                }
                $s ++ ;
            }
        }
        return $list;
    }


    /**
     * 权限校验
     * @param $class /__CLASS__
     * @param $function /__FUNCTION__
     * @return bool
     */
    protected function check_rule($class,$function)
    {

        $is_yes = false;

        if($class && $function){

            $rule = $this->redis[$this->account]['rule'];

            foreach($rule as $k => $v){
                if($class == substr($v['route'],0,strrpos($v['route'],'/'))){
                    if($function == $v['code']){
                        $is_yes = true;
                    }
                }
            }
        }

        return $is_yes;
    }

}